<?php
/**
 * created by.
 * User: EDZ
 * Date: 2020/9/29
 * TIME: 14:35
 */

namespace app\admin_libaray\user\service;

use app\libaray\service\log_service\login\LoginLog;
use app\service;

class LoginService extends service {

    /**
     * 登录服务
     *
     * @param $username
     * @param null $password
     * @param null $code
     * @return bool
     * @throws \Exception
     */
    public function authLogin($username,$password = null,$code = null){
        if (empty($username)) throw new \Exception('手机号不能为空!');
        if (empty($password) && empty($code)) throw new \Exception('必填项不能为空!');

        $userInfo = UserService::getUserInfo($username,'username');

        if (empty($userInfo)) throw new \Exception('账号或密码错误!');
        if ($userInfo['password'] != $password) throw new \Exception('密码错误');

        $loginLog = [
            'username' => $username,
            'last_login_time' => time(),
            'last_ip'   => getip(),
            'message'   =>  '登录成功!',
            'status'    =>  2
        ];
        LoginLog::createLog($loginLog);

        return true;
    }
}